package cn.bx.bid.service;


import cn.bx.bid.entity.User;
import cn.bx.bid.repository.UserRepository;
import cn.bx.bid.util.StringKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = false)
public class UserService {
    @Autowired
    UserRepository userRepository;

    //分页
    public Page<User> paging(int pageNo, int pageSize, String name, Date start,Date end){
        Sort sort =Sort.by(Sort.Order.desc("id"));

        PageRequest p=PageRequest.of(pageNo,pageSize,sort);
        return  userRepository.findAll(new Specification<User>(){
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (name != null) {
                    predicateList.add((criteriaBuilder.like(root.get("name"), "%" + name + "%")));

                }
/*                if (telephone != null) {
                    predicateList.add((criteriaBuilder.like(root.get("telephone"), "%" + telephone + "%")));

                }*/

                if (start != null) {
                    Predicate condition = criteriaBuilder.greaterThanOrEqualTo(root.get("enterdate"), start);

                    predicateList.add(condition);
                }
                if (end != null) {//<=
                    predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("enterdate"), end));
                }
                    criteriaQuery.where(predicateList.toArray(new Predicate[0]));
                    return criteriaQuery.getRestriction();



            }

    },p);
    }

    //模糊查询
    public List<User> search(String name){
        return  userRepository.findByNameContaining(name);
    }

    //增加
    public int  add(User user){
       User user1= userRepository.save(user);
       return 1;
    }
    //修改
    public int  updata(User user){
        User user1=userRepository.saveAndFlush(user);
        return 1;
    }

    //删除
    public  int delete(User user){
        userRepository.delete(user);
        return 1;
    }

    //
    public User get(long id){
    return    userRepository.getOne(id);

    }
    //验证登录
    public User login(String username,String password){
       return userRepository.findByUserNameAndPassword(username,password);
    }

    public int batchDel(long[] ids) {
        for (long id : ids) {
            User u = new User();
            u.setId(id);
            this.userRepository.delete(u);//对象
        }
        return ids.length;
    }

    //重置密码
    public int resetPass(long id,String newPass){
        return userRepository.resetPass(id,newPass);
    }
/*    public User findByUsername(String n){
        return userRepository.chazhaoByNAme(n);
    }*/
    //验证登录名是否重复
    public boolean checkUserNameRepeat(Long id, String username) {
        User u1=userRepository.findByUserName(username);
        if(u1==null)
            return false;//没重复
        if(id!=null&&u1.getId()==id)
            return false;//排除在外,没重复
        return true;
    }
}
